import {Repository} from 'typeorm';
import {AccountEntity} from './../../../account/entities/account.entity';
import {Injectable, HttpException, HttpStatus} from '@nestjs/common';
import {LoginInput} from '../../resolvers/login/dto/login.input';
import {InjectRepository} from '@nestjs/typeorm';
import {JwtService} from '@nestjs/jwt';
import {AuthService} from "src/modules/auth/auth/auth/auth.service";

@Injectable()
export class LoginService {
    constructor(
        @InjectRepository(AccountEntity)
        private readonly accountRepository: Repository<AccountEntity>,
        private readonly authService: AuthService,
    ) {
    }

    async login(loginInput: LoginInput): Promise<AccountEntity> {
        const {username, password} = loginInput;
        console.log(4444, username, password);
        const account = await this.accountRepository.findOne({where: {username:username, password:password}});
        // const token1 = this.authService.generateToken(account)
        // console.log(123123123, token1);
        const token: string = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MjY2NzM4MDMsImV4cCI6MTYyNzI3ODYwM30.7zomzq4Td6C_sAdd-bM_d1K1tr0eMkWgEQfzi543OZc'
        console.log(this.authService.verifyToken(token), '解密出来的数据');
        console.log(13123123123, account);
        if (account) {
            return Object.assign(account, {
                token: this.authService.generateToken({ id: account.id, platform: 'admin-web' }),
            });
        } else {
            throw new HttpException('账号或密码错误', HttpStatus.OK);
        }
    }

}
